package com.googlecode.milib.tools {
	import com.googlecode.milib.base.MLBaseClass;
	import com.googlecode.milib.data.value.MLNumberValue;
	import com.googlecode.milib.vo.MLEvent;

	import flash.utils.Dictionary;

	/**
	 * @author Marek Brun
	 */
	public class MLDynamicNumberUpdater extends MLBaseClass {
		
		static private var instance:MLDynamicNumberUpdater;
		private var dictNumber_ArrayObjParam:Dictionary;

		public function MLDynamicNumberUpdater(access:Private) {
			dictNumber_ArrayObjParam=new Dictionary();
		}
		
		private function _addUpdate(number:MLNumberValue, obj:Object, propertyName:String):void {
			dictNumber_ArrayObjParam[number]=[obj, propertyName];
			number.v=obj[propertyName];
			number.addEventListener(number.event_Changed, onNumber_Changed);
		}
		
		static public function addUpdate(number:MLNumberValue, obj:Object, propertyName:String):void {
			getInstance()._addUpdate(number, obj, propertyName);
		}
		
		static public function addUpdateForID(numberID:String, obj:Object, propertyName:String):void {
			getInstance()._addUpdate(MLNumberValue.getInstanceForID(numberID), obj, propertyName);
		}
		
		static private function getInstance():MLDynamicNumberUpdater {
			if(instance) { return instance; }
			else{ instance=new MLDynamicNumberUpdater(null); return instance; }
		}
		
//********************************************************************************************
//		events for MLDynamicNumberUpdater
//********************************************************************************************
		protected function onNumber_Changed(event:MLEvent):void {
			var number:MLNumberValue=MLNumberValue(event.info.hero);
			dictNumber_ArrayObjParam[number][0][dictNumber_ArrayObjParam[number][1]]=number.v;
		}
		
	}
}
internal class Private {}